package com.jrapider;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrapider.designer.service.FreemarkService;
import com.jrapider.designer.service.SysEntityFieldService;
import com.jrapider.designer.service.SysEntityService;
import com.jrapider.designer.service.SysPageService;
import com.jrapider.om.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2018/11/5.
 */
@Controller
public class IndexController {
    @Autowired
    private SysEntityService sysEntityService;
    @Autowired
    private SysEntityFieldService sysEntityFieldService;
    @Autowired
    private SysPageService sysPageService;

    @RequestMapping("")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "index";
    }

    @RequestMapping("/entity")
    public String entity(@RequestParam(required = false) Long entityId, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<SysEntity> entities = sysEntityService.list(new QueryWrapper<>(new SysEntity()));
        model.addAttribute("entities", entities);
        if (entityId == null && entities.size() > 0) {
            entityId = entities.get(0).getId();
        }
        SysEntityField condition = new SysEntityField();
        condition.setEntityId(entityId);
        SysEntity sysEntity = sysEntityService.getById(entityId);
        List<SysEntityField> entityFields = sysEntityFieldService.list(new QueryWrapper<>(condition));
        model.addAttribute("entityFields", entityFields);
        model.addAttribute("entityId", entityId);
        model.addAttribute("module", sysEntity.getModule());
        return "entity";
    }
    @RequestMapping("/project")
    public String project() {
        return "project";
    }

    @Autowired
    private FreemarkService freemarkService;
    @RequestMapping("/project/build")
    @ResponseBody
    public JSONObject buildProject(String projectName,String group) {
        freemarkService.buildProject(projectName,group);
        return JSONResult.newBuilder()
                .setCode(JSONResult.SUCCESS)
                .setMsg("生成成功")
                .build();

    }
    @Data
    static class EntitySaveModel{
        private List<SysEntityField> sysEntityFields;
        private Long entityId;
        private String module;
    }

    @RequestMapping("/entity/save")
    @ResponseBody
    public JSONObject entitySave(@RequestBody EntitySaveModel reqModel, HttpServletRequest request, HttpServletResponse response, Model model) {
        SysEntity sysEntity = new SysEntity();
        sysEntity.setId(reqModel.getEntityId());
        sysEntity.setModule(reqModel.getModule());
        if (sysEntityFieldService.saveOrUpdateBatch(sysEntity,reqModel.getSysEntityFields())) {
            return JSONResult.newBuilder()
                    .setCode(JSONResult.SUCCESS)
                    .setMsg("保存成功")
                    .build();
        } else {
            return JSONResult.newBuilder()
                    .setCode(JSONResult.FAIL)
                    .setMsg("保存失败")
                    .build();
        }

    }

    @RequestMapping("/page")
    public String page(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "page_list";
    }

    @RequestMapping("/page/query")
    @ResponseBody
    public JSONObject pageQuery(SysPage sysPage, PageInfo pageInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
        IPage<SysPage> pageResult = sysPageService.page(pageInfo.conventToMybatisPage(SysPage.class), new QueryWrapper<>(sysPage));
        return JSONResult.newBuilder()
                .setCode(JSONResult.SUCCESS)
                .setRows(pageResult.getRecords())
                .setCount(pageResult.getTotal())
                .setMsg("查询成功")
                .build();
    }

    @RequestMapping("/page/edit")
    public String pageEdit(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        SysPage sysPage = null;
        if (id != null) {
            sysPage = sysPageService.getById(id);
        }
        model.addAttribute("data", sysPage == null ? new SysPage() : sysPage);
        return "page_edit";
    }
    @RequestMapping("/page/del")
    @ResponseBody
    public JSONObject pageDel(@RequestParam Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (sysPageService.removeById(id)) {
            return JSONResult.newBuilder()
                    .setCode(JSONResult.SUCCESS)
                    .setMsg("删除成功")
                    .build();
        } else {
            return JSONResult.newBuilder()
                    .setCode(JSONResult.FAIL)
                    .setMsg("删除失败")
                    .build();
        }
    }
    @RequestMapping("/page/save")
    @ResponseBody
    public JSONObject pageEdit(SysPage sysPage, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (sysPageService.saveOrUpdate(sysPage)) {
            return JSONResult.newBuilder()
                    .setCode(JSONResult.SUCCESS)
                    .setMsg("保存成功")
                    .setData(sysPage.getId())
                    .build();
        } else {
            return JSONResult.newBuilder()
                    .setCode(JSONResult.FAIL)
                    .setMsg("保存失败")
                    .build();
        }
    }
    @RequestMapping("/page/gencode")
    @ResponseBody
    public JSONObject gencode(SysPage sysPage, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (sysPageService.saveOrUpdate(sysPage)) {
            return sysPageService.genCode(sysPage);
        } else {
            return JSONResult.newBuilder()
                    .setCode(JSONResult.FAIL)
                    .setMsg("保存失败")
                    .build();
        }
    }
}
